/**
 * 
 */
package a0;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author totyu
 *
 */
public class PX2SIZE481 {

    public static double s2d(String text) {
        return s2d(text, -1D);
    }

    public static double s2d(String text, double defaultValue) {
        try {
            return Double.parseDouble(Pattern.compile("[^0-9]").matcher(text).replaceAll("").trim());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static void main(String[] args) {
        // 创建一个正则表达式模式，用以匹配一个单词
        String pattern = "(\\d+)px";

        // 用于测试的输入字符串
        String input = "                        text-shadow: rgba(0, 0, 0, 0.47) 0px 1px 0px;";
        System.out.println("Input:" + input);

        // 从正则表达式实例中运行方法并查看其如何运行
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(input);

        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String key = m.group(1);
            String size = null;
            double i = Double.parseDouble(key);
            if (i == 0D) {
                size = "0";
            } else {
                size = "size(" + (i * 480 / 375) + ")";
            }
            m.appendReplacement(sb, size == null ? "" : size);
        }
        m.appendTail(sb);
        System.out.println(sb.toString());

//        double i = 0;
//        if (m.find()) {
//            i = s2d(m.group(0));
//            System.out.println(i);
//        }
//        String size = "size(" + (i * 480 / 375) + ")";
//        String dist = m.replaceAll(size);
//        System.out.println("ReplaceAll:" + dist);
//
//        // appendReplacement方法
//        m.reset();
//
//        String size0 = "size(0.0)";
//        dist = dist.replaceAll(size0, "0");
//        System.out.println("dist:" + dist);

//        StringBuffer sb = new StringBuffer();
//        while (m.find()) {
//            // 将匹配之前的字符串复制到sb,再将匹配结果替换为："favour"，并追加到sb
//            m.appendReplacement(sb, size);
//        }
//        System.out.println(sb.toString());
//        m.appendTail(sb);
//        System.out.println(sb.toString());
    }
}
